package com.njupt.huawei;

import java.io.*;
import java.util.*;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/4/16 19:24
 * @Description:
 * @Version: 1.0
 */


public class Main2_2 {

    static class State {
        String station;
        int line;
        int transferCount;
        State prev;
        boolean isTransfer;

        public State(String station, int line, int transferCount, State prev, boolean isTransfer) {
            this.station = station;
            this.line = line;
            this.transferCount = transferCount;
            this.prev = prev;
            this.isTransfer = isTransfer;
        }
    }

    public static void main(String[] args) throws IOException {


        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        List<List<String>> lines = new ArrayList<>();
        Map<String, List<Integer>> stationLines = new HashMap<>();
        List<Map<String, Integer>> stationIndexInLine = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            String line = br.readLine().trim();
            if (line.isEmpty()) {
                line = br.readLine().trim();
            }
            String[] parts = line.split(" +");
            List<String> lineStations = new ArrayList<>(Arrays.asList(parts));
            lines.add(lineStations);
            Map<String, Integer> indexMap = new HashMap<>();
            for (int j = 0; j < lineStations.size(); j++) {
                String station = lineStations.get(j);
                indexMap.put(station, j);
                stationLines.computeIfAbsent(station, k -> new ArrayList<>()).add(i);
            }
            stationIndexInLine.add(indexMap);
        }

        String[] se = br.readLine().trim().split(" +");
        String start = se[0];
        String end = se[1];

        if (start.equals(end)) {
            System.out.println(start + "-" + end);
            System.out.println(0);
            return;
        }

        if (!stationLines.containsKey(start) || !stationLines.containsKey(end)) {
            System.out.println("NA");
            return;
        }

        Queue<State> queue = new LinkedList<>();
        Map<String, Map<Integer, Integer>> visited = new HashMap<>();

        List<Integer> startLineList = stationLines.get(start);
        for (int line : startLineList) {
            visited.computeIfAbsent(start, k -> new HashMap<>()).put(line, 0);
            queue.add(new State(start, line, 0, null, false));
        }

        State result = null;

        while (!queue.isEmpty()) {
            State current = queue.poll();

            if (current.station.equals(end)) {
                result = current;
                break;
            }

            List<String> currentLine = lines.get(current.line);
            int currentIndex = stationIndexInLine.get(current.line).get(current.station);

            if (currentIndex > 0) {
                String prevStation = currentLine.get(currentIndex - 1);
                processNeighbor(current, prevStation, current.line, queue, visited, false);
            }

            if (currentIndex < currentLine.size() - 1) {
                String nextStation = currentLine.get(currentIndex + 1);
                processNeighbor(current, nextStation, current.line, queue, visited, false);
            }

            List<Integer> transferLines = stationLines.get(current.station);
            for (int line : transferLines) {
                if (line == current.line) continue;
                int newTransferCount = current.transferCount + 1;
                if (checkVisited(current.station, line, newTransferCount, visited)) {
                    State newState = new State(current.station, line, newTransferCount, current, true);
                    queue.add(newState);
                    updateVisited(current.station, line, newTransferCount, visited);
                }
            }
        }

        if (result == null) {
            System.out.println("NA");
        } else {
            List<String> transfers = new ArrayList<>();
            State s = result;
            while (s != null) {
                if (s.isTransfer && s.prev != null) {
                    transfers.add(s.station);
                }
                s = s.prev;
            }
            Collections.reverse(transfers);

            StringBuilder path = new StringBuilder(start);
            for (String t : transfers) {
                path.append("-").append(t);
            }
            path.append("-").append(end);

            System.out.println(path);
            System.out.println(2 + transfers.size());
        }
    }

    private static void processNeighbor(State current, String neighbor, int line, Queue<State> queue,
                                        Map<String, Map<Integer, Integer>> visited, boolean isTransfer) {
        int newTransferCount = current.transferCount;
        if (checkVisited(neighbor, line, newTransferCount, visited)) {
            State newState = new State(neighbor, line, newTransferCount, current, isTransfer);
            queue.add(newState);
            updateVisited(neighbor, line, newTransferCount, visited);
        }
    }

    private static boolean checkVisited(String station, int line, int transferCount,
                                        Map<String, Map<Integer, Integer>> visited) {
        if (!visited.containsKey(station)) {
            return true;
        }
        Map<Integer, Integer> linesMap = visited.get(station);
        return !linesMap.containsKey(line) || linesMap.get(line) > transferCount;
    }

    private static void updateVisited(String station, int line, int transferCount,
                                      Map<String, Map<Integer, Integer>> visited) {
        visited.computeIfAbsent(station, k -> new HashMap<>()).put(line, transferCount);
    }
}
